from rest_framework.viewsets import ModelViewSet
from goods.models import SPU, Brand, GoodsCategory
from meiduo_admin.serializers.spu_serializer import SPUModelSerializer, BrandSimpleSerializer, GoodsCategorySimpleSerializer
from rest_framework.generics import ListAPIView
from meiduo_admin.pages import MyPage


class SPUViewSet(ModelViewSet):
    queryset = SPU.objects.all()
    serializer_class = SPUModelSerializer
    pagination_class = MyPage

class BrandSimpleView(ListAPIView):
    queryset = Brand.objects.all()
    serializer_class = BrandSimpleSerializer


class GoodsCategoryView(ListAPIView):
    # queryset = GoodsCategory.objects.filter(parent=None)
    queryset = GoodsCategory.objects.all()
    serializer_class = GoodsCategorySimpleSerializer

    def get_queryset(self):
        category_id = self.kwargs.get('pk')
        if category_id:
            return self.queryset.filter(parent_id=category_id)
        return self.queryset.filter(parent=None)
